package com.mvp.shq.washer.utils;

import android.database.sqlite.SQLiteDatabase;

import com.mvp.shq.washer.APP;
import com.mvp.shq.washer.entity.shop.CompanyProductRelative;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.entity.shop.ProductComment;
import com.mvp.shq.washer.entity.shop.ShopAttr;
import com.mvp.shq.washer.entity.shop.ShopSpec;
import com.mvp.shq.washer.entity.shop.TotalOrderData;
import com.mvp.shq.washer.entity.shop.TotalOrderListData;
import com.shq.mymvp.dao.CompanyProductRelativeDao;
import com.shq.mymvp.dao.DaoMaster;
import com.shq.mymvp.dao.DaoSession;
import com.shq.mymvp.dao.ProductCommentDao;
import com.shq.mymvp.dao.ProductDao;
import com.shq.mymvp.dao.ShopAttrDao;
import com.shq.mymvp.dao.ShopSpecDao;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/13.
 */

public class DBUtil {
    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private static DBUtil instance;

    public static DBUtil getInstance(){
        if(instance==null){
            synchronized (DBUtil.class){
                if(instance==null){
                    instance=new DBUtil();
                }
            }
        }
        return instance;
    }

    public DBUtil(){
        setDatabase();
    }

    public List<Product> getProductsInCart(){
        ProductDao dao = mDaoSession.getProductDao();
        return dao.loadAll();
    }

    public List<TotalOrderListData> getCompanyProductsInCart(){
        List<TotalOrderListData> data = new ArrayList<>();
        CompanyProductRelativeDao companyDao = mDaoSession.getCompanyProductRelativeDao();
        List<CompanyProductRelative> companys = companyDao.loadAll();
        if (companys == null){
            return data;
        }
        ProductDao productDao = mDaoSession.getProductDao();
        for (int i=0;i<companys.size();i++){
            TotalOrderListData orderData = new TotalOrderListData();
            String companyId = companys.get(i).getCompanyId();
            orderData.setCompanyId(companyId);
            List<Product> products = productDao.queryBuilder().where(ProductDao.Properties.CompanyId.eq(companyId)).list();
            orderData.setProducts(products);
            if (products != null && products.size() > 0){
                data.add(orderData);
            }
        }
        return data;
    }

    public void addProductToCart(List<Product> products){
        for (int i=0;i<products.size();i++){
            insertOrUpdateProduct(products.get(i));
        }

    }
    public void delProductFromCart(List<Product> products){
        for (int i=0;i<products.size();i++){
            delProduct(products.get(i));
        }
    }

    public void insertOrUpdateProduct(Product product){

        ProductDao dao = mDaoSession.getProductDao();
        Product oldProduct = dao.queryBuilder().where(ProductDao.Properties.Id.eq(product.getId())).unique();
        if (oldProduct == null){
            dao.insert(product);
            CompanyProductRelativeDao relativeDao = mDaoSession.getCompanyProductRelativeDao();
            CompanyProductRelative relative = relativeDao.queryBuilder().where(CompanyProductRelativeDao.Properties.CompanyId.eq(product.getCompanyId())).unique();
            if (relative == null){
                //之前没有该商户的商品
                CompanyProductRelative companyProductRelative = new CompanyProductRelative();
                companyProductRelative.setCompanyId(product.getCompanyId());
                relativeDao.insert(companyProductRelative);
            }
            return;
        }
        if (product.getId().equals(oldProduct.getId())){
            int byNum = oldProduct.getByNum() + product.getByNum();
            oldProduct.setByNum(byNum);
            dao.update(oldProduct);
        }
//        ProductComment comment = product.getComment();
//        if (comment != null){
//            comment.setProductId(product.getId());
//            insertOrUpdateComment(comment);
//        }
//        List<ShopSpec> shopSpecs = product.getSpecList();
//        for (int i=0;i<shopSpecs.size();i++){
//            ShopSpec spec = shopSpecs.get(i);
//            spec.setProductId(product.getId());
//            insertOrUpdateSpec(spec);
//            List<ShopAttr> attrs = spec.getAttrs();
//            for (int j=0;j<attrs.size();i++){
//                ShopAttr attr = attrs.get(j);
//                attr.setSpecId(spec.getId());
//                insertOrUpdateAttr(attr);
//            }
//        }
    }
    public void UpdateProduct(Product product){

        ProductDao dao = mDaoSession.getProductDao();
        Product oldProduct = dao.queryBuilder().where(ProductDao.Properties.Id.eq(product.getId())).unique();
        if (oldProduct == null){
            dao.insert(product);
            CompanyProductRelativeDao relativeDao = mDaoSession.getCompanyProductRelativeDao();
            CompanyProductRelative relative = relativeDao.queryBuilder().where(CompanyProductRelativeDao.Properties.CompanyId.eq(product.getCompanyId())).unique();
            if (relative == null){
                //之前没有该商户的商品
                CompanyProductRelative companyProductRelative = new CompanyProductRelative();
                companyProductRelative.setCompanyId(product.getCompanyId());
                relativeDao.insert(companyProductRelative);
            }
            return;
        }
        if (product.getId().equals(oldProduct.getId())){
            int byNum =product.getByNum();
            oldProduct.setByNum(byNum);
            dao.update(oldProduct);
        }
    }
    public void insertOrUpdateComment(ProductComment comment){
        ProductCommentDao dao = mDaoSession.getProductCommentDao();
        dao.insertOrReplace(comment);
    }
    public void insertOrUpdateSpec(ShopSpec shopSpec){
        ShopSpecDao dao = mDaoSession.getShopSpecDao();
        dao.insertOrReplace(shopSpec);
    }
    public void insertOrUpdateAttr(ShopAttr shopAttr){
        ShopAttrDao dao = mDaoSession.getShopAttrDao();
        dao.insertOrReplace(shopAttr);
    }

    public void delProductById(List<String> ids){
        if (ids == null || ids.size() <= 0){
            return;
        }
        for (int i=0;i<ids.size();i++){
            delProductById(ids.get(i));
        }
    }

    public void delProductById(String productId){
        ProductDao dao = mDaoSession.getProductDao();
        List<Product> products = dao.loadAll();
        if (products == null){
            return;
        }
        for (int i=0;i<products.size();i++){
            Product product = products.get(i);
            if (productId.equals(product.getId())){
                dao.delete(product);
            }
        }
    }

    public void delProduct(Product product){
        ProductDao dao = mDaoSession.getProductDao();
        dao.delete(product);
//        ProductComment comment = product.getComment();
//        if (comment != null){
//            comment.setProductId(product.getId());
//            delComment(comment);
//        }
//        List<ShopSpec> shopSpecs = product.getSpecList();
//        if (shopSpecs != null && shopSpecs.size() > 0){
//            for (int i=0;i<shopSpecs.size();i++){
//                ShopSpec spec = shopSpecs.get(i);
//                spec.setProductId(product.getId());
//                delSpec(spec);
//                List<ShopAttr> attrs = spec.getAttrs();
//                for (int j=0;j<attrs.size();i++){
//                    ShopAttr attr = attrs.get(j);
//                    attr.setSpecId(spec.getId());
//                    delAttr(attr);
//                }
//            }
//        }
    }
    public void delComment(ProductComment comment){
        ProductCommentDao dao = mDaoSession.getProductCommentDao();
        dao.delete(comment);
    }
    public void delSpec(ShopSpec shopSpec){
        ShopSpecDao dao = mDaoSession.getShopSpecDao();
        dao.delete(shopSpec);
    }
    public void delAttr(ShopAttr shopAttr){
        ShopAttrDao dao = mDaoSession.getShopAttrDao();
        dao.delete(shopAttr);
    }

    public void clearCart(){
        ProductDao dao = mDaoSession.getProductDao();
        dao.deleteAll();
        CompanyProductRelativeDao dao1 = mDaoSession.getCompanyProductRelativeDao();
        dao1.deleteAll();
    }

    /**
     * 设置greenDao
     */
    private void setDatabase() {
        // 通过 DaoMaster 的内部类 DevOpenHelper，你可以得到一个便利的 SQLiteOpenHelper 对象。
        // 可能你已经注意到了，你并不需要去编写「CREATE TABLE」这样的 SQL 语句，因为 greenDAO 已经帮你做了。
        // 注意：默认的 DaoMaster.DevOpenHelper 会在数据库升级时，删除所有的表，意味着这将导致数据的丢失。
        // 所以，在正式的项目中，你还应该做一层封装，来实现数据库的安全升级。
        mHelper = new DaoMaster.DevOpenHelper(APP.getApp(), "washer-db", null);
        db = mHelper.getWritableDatabase();
        // 注意：该数据库连接属于 DaoMaster，所以多个 Session 指的是相同的数据库连接。
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }

}
